{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train faster, more flexible models with Amazon SageMaker Linear Learner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Today Amazon SageMaker is launching several additional features to the built-in linear learner algorithm.  Amazon SageMaker algorithms are designed to scale effortlessly to massive datasets and take advantage of the latest hardware optimizations for unparalleled speed.  The Amazon SageMaker linear learner algorithm encompasses both linear regression and binary classification algorithms.  These algorithms are used extensively in banking, fraud/risk management, insurance, and healthcare.  The new features of linear learner are designed to speed up training and help you customize models for different use cases.  Examples include classification with unbalanced classes, where one of your outcomes happens far less frequently than another.  Or specialized loss functions for regression, where it’s more important to penalize certain model errors more than others.\n",
    "\n",
    "In this blog post we'll cover three things:\n",
    "1. Early stopping and saving the best model\n",
    "1. New ways to customize linear learner models, including:\n",
    "   * Hinge loss (support vector machines)\n",
    "   * Quantile loss\n",
    "   * Huber loss\n",
    "   * Epsilon-insensitive loss\n",
    "   * Class weights options\n",
    "1. Then we'll walk you through a hands-on example of using class weights to boost performance in binary classification"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Early Stopping\n",
    "\n",
    "Linear learner trains models using Stochastic Gradient Descent (SGD) or variants of SGD like Adam.  Training requires multiple passes over the data, called *epochs*, in which the data are loaded into memory in chunks called *batches*, sometimes called *minibatches*.  How do we know how many epochs to run?  Ideally, we'd like to continue training until convergence - that is, until we no longer see any additional benefits.  Running additional epochs after the model has converged is a waste of time and money, but guessing the right number of epochs is difficult to do before submitting a training job.  If we train for too few epochs, our model will be less accurate than it should be, but if we train for too many epochs, we'll waste resources and potentially harm model accuracy by overfitting.  To remove the guesswork and optimize model training, linear learner has added two new features: automatic early stopping and saving the best model.  \n",
    "\n",
    "Early stopping works in two basic regimes: with or without a validation set.  Often we split our data into training, validation, and testing data sets.  Training is for optimizing the loss, validation is for tuning hyperparameters, and testing is for producing an honest estimate of how the model will perform on unseen data in the future.  If you provide linear learner with a validation data set, training will stop early when validation loss stops improving.  If no validation set is available, training will stop early when training loss stops improving.\n",
    "\n",
    "#### Early Stopping with a validation data set\n",
    "One big benefit of having a validation data set is that we can tell if and when we start overfitting to the training data.  Overfitting is when the model gives predictions that are too closely tailored to the training data, so that generalization performance (performance on future unseen data) will be poor.  The following plot on the right shows a typical progression during training with a validation data set.  Until epoch 5, the model has been learning from the training set and doing better and better on the validation set.  But in epochs 7-10, we see that the model has begun to overfit on the training set, which shows up as worse performance on the validation set.  Regardless of whether the model continues to improve (overfit) on the training data, we want to stop training after the model starts to overfit.  And we want to restore the best model from just before the overfitting started.  These two features are now turned on by default in linear learner.  \n",
    "\n",
    "The default parameter values for early stopping are shown in the following code.  To tweak the behavior of early stopping, try changing the values.  To turn off early stopping entirely, choose a patience value larger than the number of epochs you want to run."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    early_stopping_patience=3,\n",
    "    early_stopping_tolerance=0.001,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The parameter early_stoping_patience defines how many epochs to wait before ending training if no improvement is made.  It's useful to have a little patience when deciding to stop early, since the training curve can be bumpy.  Performance may get worse for one or two epochs before continuing to improve.  By default, linear learner will stop early if performance has degraded for three epochs in a row.\n",
    "\n",
    "The parameter early_stopping_tolerance defines the size of an improvement that's considered significant.  If the ratio of the improvement in loss divided by the previous best loss is smaller than this value, early stopping will consider the improvement to be zero."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Early stopping without a validation data set\n",
    "\n",
    "When training with a training set only, we have no way to detect overfitting.  But we still want to stop training once the model has converged and improvement has levelled off.  In the left panel of the following figure, that happens around epoch 25."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"images/early_stop.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Early stopping and calibration\n",
    "You may already be familiar with the linear learner automated threshold tuning for binary classification models.  Threshold tuning and early stopping work together seamlessly by default in linear learner.  \n",
    "\n",
    "When a binary classification model outputs a probability (e.g., logistic regression) or a raw score (SVM), we convert that to a binary prediction by applying a threshold, for example:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predicted_label = 1 if raw_prediction > 0.5 else 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We might want to tune the threshold (0.5 in the example) based on the metric we care about most, such as accuracy or recall.  Linear learner does this tuning automatically using the 'binary_classifier_model_selection_criteria' parameter.  When threshold tuning and early stopping are both turned on (the default), then training stops early based on the metric you request.  For example, if you provide a validation data set and request a logistic regression model with threshold tuning based on accuracy, then training will stop when the model with auto-thresholding reaches optimal performance on the validation data.  If there is no validation set and auto-thresholding is turned off, then training will stop when the best value of the loss function on the training data is reached."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## New loss functions\n",
    "\n",
    "The loss function is our definition of the cost of making an error in prediction.  When we train a model, we push the model weights in the direction that minimizes loss, given the known labels in the training set.  The most common and well-known loss function is squared loss, which is minimized when we train a standard linear regression model.  Another common loss function is the one used in logistic regression, variously known as logistic loss, cross-entropy loss, or binomial likelihood.  Ideally, the loss function we train on should be a close match to the business problem we're trying to solve.  Having the flexibility to choose different loss functions at training time allows us to customize models to different use cases.  In this section, we'll discuss when to use which loss function, and introduce several new loss functions that have been added to linear learner."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"images/loss_functions.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Squared loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='regressor',\n",
    "    loss='squared_loss',"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^{N} (w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i)^2$$\n",
    "\n",
    "We'll use the following notation in all of the loss functions we discuss:\n",
    "\n",
    "$w_0$ is the bias that the model learns\n",
    "\n",
    "$\\mathbf{w}$ is the vector of feature weights that the model learns\n",
    "\n",
    "$y_i$ and $\\mathbf{x_i}$ are the label and feature vector, respectively, from example $i$ of the training data\n",
    "\n",
    "$N$ is the total number of training examples\n",
    "\n",
    "Squared loss is a first choice for most regression problems.  It has the nice property of producing an estimate of the mean of the label given the features.  As seen in the plot above, squared loss implies that we pay a very high cost for very wrong predictions.  This can cause problems if our training data include some extreme outliers.  A model trained on squared loss will be very sensitive to outliers.  Squared loss is sometimes known as mean squared error (MSE), ordinary least squares (OLS), or $\\text{L}_2$ loss.   Read more about [squared loss](https://en.wikipedia.org/wiki/Least_squares) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Absolute loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='regressor',\n",
    "    loss='absolute_loss',"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^{N} |w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i|$$\n",
    "\n",
    "Absolute loss is less common than squared loss, but can be very useful.  The main difference between the two is that training a model on absolute loss will produces estimates of the median of the label given the features.  Squared loss estimates the mean, and absolute loss estimates the median.  Whether you want to estimate the mean or median will depend on your use case.  Let's look at a few examples:\n",
    "* If an error of -2 costs you \\$2 and an error of +50 costs you \\$50, then absolute loss models your costs better than squared loss.  \n",
    "* If an error of -2 costs you \\$2, while an error of +50 is simply unacceptably large, then it's important that your errors are generally small, and so squared loss is probably the right fit.  \n",
    "* If it's important that your predictions are too high as often as they're too low, then you want to estimate the median with absolute loss.  \n",
    "* If outliers in your training data are having too much influence on the model, try switching from squared to absolute loss.  Large errors get a large amount of attention from absolute loss, but with squared loss, large errors get squared and become huge errors attracting a huge amount of attention.  If the error is due to an outlier, it might not deserve a huge amount of attention.\n",
    "\n",
    "Absolute loss is sometimes also known as $\\text{L}_1$ loss or least absolute error.  Read more about [absolute loss](https://en.wikipedia.org/wiki/Least_absolute_deviations) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quantile loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='regressor',\n",
    "    loss='quantile_loss',\n",
    "    quantile=0.9,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N q(y_i - w_o - \\mathbf{x_i}^\\intercal \\mathbf{w})^\\text{+} + (1-q)(w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i)^\\text{+} $$\n",
    "\n",
    "$$ \\text{where the parameter } q \\text{ is the quantile you want to predict}$$\n",
    "\n",
    "Quantile loss lets us predict an upper or lower bound for the label, given the features. To make predictions that are larger than the true label 90% of the time, train quantile loss with the 0.9 quantile. An example would be predicting electricity demand where we want to build near peak demand since building to the average would result in brown-outs and upset customers.  Read more about [quantile loss](https://en.wikipedia.org/wiki/Quantile_regression) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Huber loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='regressor',\n",
    "    loss='huber_loss',\n",
    "    huber_delta=0.5,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "$$ \\text{Let the error be } e_i = w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i \\text{.  Then Huber loss solves:}$$\n",
    "\n",
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N I(|e_i| < \\delta) \\frac{e_i^2}{2} + I(|e_i| >= \\delta) |e_i|\\delta - \\frac{\\delta^2}{2} $$\n",
    "\n",
    "$$ \\text{where } I(a) = 1 \\text{ if } a \\text{ is true, else } 0 $$\n",
    "\n",
    "Huber loss is an interesting hybrid of $\\text{L}_1$ and $\\text{L}_2$ losses.  Huber loss counts small errors on a squared scale and large errors on an absolute scale.  In the plot above, we see that Huber loss looks like squared loss when the error is near 0 and absolute loss beyond that.  Huber loss is useful when we want to train with squared loss, but want to avoid squared loss's sensitivity to outliers.  Huber loss gives less importance to outliers by not squaring the larger errors.  Read more about [Huber loss](https://en.wikipedia.org/wiki/Huber_loss) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Epsilon-insensitive loss\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='regressor',\n",
    "    loss='eps_insensitive_squared_loss',\n",
    "    loss_insensitivity=0.25,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "For epsilon-insensitive squared loss, we minimize\n",
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N max(0, (w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i)^2 - \\epsilon^2) $$\n",
    "\n",
    "And for epsilon-insensitive absolute loss, we minimize\n",
    "\n",
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N max(0, |w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w} - y_i| - \\epsilon) $$\n",
    "\n",
    "Epsilon-insensitive loss is useful when errors don't matter to you as long as they're below some threshold.  Set the threshold that makes sense for your use case as epsilon.  Epsilon-insensitive loss will allow the model to pay no cost for making errors smaller than epsilon."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Logistic regression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='binary_classifier',\n",
    "    loss='logistic',\n",
    "    binary_classifier_model_selection_criteria='recall_at_target_precision',\n",
    "    target_precision=0.9,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each of the losses we've discussed is for regression problems, where the labels are floating point numbers.  The last two losses we'll cover, logistic regression and support vector machines, are for binary classification problems where the labels are one of two classes.  Linear learner expects the class labels to be 0 or 1.  This may require some preprocessing, for example if your labels are coded as -1 and +1, or as blue and yellow.  Logistic regression produces a predicted probability for each data point:\n",
    "\n",
    "$$ p_i = \\sigma(w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w}) $$\n",
    "\n",
    "The loss function minimized in training a logistic regression model is the log likelihood of a binomial distribution.  It assigns the highest cost to predictions that are confident and wrong, for example a prediction of 0.99 when the true label was 0, or a prediction of 0.002 when the true label was positive.  The loss function is:\n",
    "\n",
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N y_i  \\text{log}(p) - (1 - y_i) \\text{log}(1 - p) $$  \n",
    "\n",
    "$$ \\text{where } \\sigma(x) = \\frac{\\text{exp}(x)}{1 + \\text{exp}(x)}  $$\n",
    "\n",
    "Read more about [logistic regression](https://en.wikipedia.org/wiki/Logistic_regression) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hinge loss (support vector machine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    predictor_type='binary_classifier',\n",
    "    loss='hinge_loss',\n",
    "    margin=1.0,\n",
    "    binary_classifier_model_selection_criteria='recall_at_target_precision',\n",
    "    target_precision=0.9,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another popular option for binary classification problems is the hinge loss, also known as a Support Vector Machine (SVM) or Support Vector Classifier (SVC) with a linear kernel.  It places a high cost on any points that are misclassified or nearly misclassified.  To tune the meaning of \"nearly\", adjust the margin parameter:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's difficult to say in advance whether logistic regression or SVM will be the right model for a binary classification problem, though logistic regression is generally a more popular choice then SVM.  If it's important to provide probabilities of the predicted class labels, then logistic regression will be the right choice.  If all that matters is better accuracy, precision, or recall, then either model may be appropriate.  One advantage of logistic regression is that it produces the probability of an example having a positive label.  That can be useful, for example in an ad serving system where the predicted click probability is used as an input to a bidding mechanism.  Hinge loss does not produce class probabilities.\n",
    "\n",
    "Whichever model you choose, you're likely to benefit from linear learner's options for tuning the threshold that separates positive from negative predictions\n",
    "\n",
    "$$\\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^{N} y_i(\\frac{m+1}{2} - w_0 - \\mathbf{x_i}^\\text{T}\\mathbf{w})^\\text{+} + (1-y_i)\\frac{m-1}{2} + w_o + \\mathbf{x_i}^\\text{T}\\mathbf{w})^\\text{+}$$\n",
    "\n",
    "$$\\text{where  } a^\\text{+} = \\text{max}(0, a)$$\n",
    "\n",
    "\n",
    "Note that the hinge loss we use is a reparameterization of the usual hinge loss: typically hinge loss expects the binary label to be in {-1, 1}, whereas ours expects the binary labels to be in {0, 1}.  This reparameterization allows LinearLearner to accept the same data format for binary classification regardless of the training loss.  Read more about [hinge loss](https://en.wikipedia.org/wiki/Hinge_loss) on wikipedia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Class weights\n",
    "In some binary classification problems, we may find that our training data is highly unbalanced.  For example, in credit card fraud detection, we're likely to have many more examples of non-fraudulent transactions than fraudulent.  In these cases, balancing the class weights may improve model performance.\n",
    " \n",
    "Suppose we have 98% negative and 2% positive examples.  To balance the total weight of each class, we can set the positive class weight to be 49.  Now the average weight from the positive class is 0.98 $\\cdot$ 1 = 0.98, and the average weight from the negative class is 0.02 $\\cdot$ 49 = 0.98.  The negative class weight multiplier is always 1.\n",
    " \n",
    "To incorporate the positive class weight in training, we multiply the loss by the positive weight whenever we see a positive class label.  For logistic regression, the weighted loss is:\n",
    "\n",
    "Weighted logistic regression:\n",
    "\n",
    "$$ \\text{argmin}_{w_0, \\mathbf{w}} \\sum_{i=1}^N p y_i  \\text{log}(\\sigma(w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w})) - (1 - y_i) \\text{log}(1 - \\sigma(w_0 + \\mathbf{x_i}^\\intercal \\mathbf{w})) $$  \n",
    "\n",
    "$$ \\text{where } p \\text{ is the weight for the positive class.} $$\n",
    " \n",
    "The only difference between the weighted and unweighted logistic regression loss functions is the presense of the class weight, $p$ on the left-hand term in the loss.  Class weights in the hinge loss (SVM) classifier are applied in the same way.\n",
    "\n",
    "To apply class weights when training a model with linear learner, supply the weight for the positive class as a training parameter:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "    positive_example_weight_mult=200,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Or to ask linear learner to calculate the positive class weight for you:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "    positive_example_weight_mult='balanced',"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hands-on example: Detecting credit card fraud\n",
    "\n",
    "In this section, we'll look at a credit card fraud detection dataset.  The data set (Dal Pozzolo et al. 2015) was downloaded from [Kaggle](https://www.kaggle.com/mlg-ulb/creditcardfraud/data).  We have features and labels for over a quarter million credit card transactions, each of which is labeled as fraudulent or not fraudulent.  We'd like to train a model based on the features of these transactions so that we can predict risky or fraudulent transactions in the future.  This is a binary classification problem.  \n",
    "\n",
    "We'll walk through training linear learner with various settings and deploying an inference endpoint.  We'll evaluate the quality of our models by hitting that endpoint with observations from the test set.  We can take the real-time predictions returned by the endpoint and evaluate them against the ground-truth labels in our test set.\n",
    "\n",
    "Next, we'll apply the linear learner threshold tuning functionality to get better precision without sacrificing recall.  Then, we'll push the precision even higher using the linear learner new class weights feature.  Because fraud can be extremely costly, we would prefer to have high recall, even if this means more false positives.  This is especially true if we are building a first line of defense, flagging potentially fraudulent transactions for further review before taking actions that affect customers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we'll do some preprocessing on this data set: we'll shuffle the examples and split them into train and test sets.  To run this under notebook under your own AWS account, you'll need to change the Amazon S3 locations.  First download the raw data from [Kaggle](https://www.kaggle.com/mlg-ulb/creditcardfraud/data) and upload to your SageMaker notebook instance (or wherever you're running this notebook).  Only 0.17% of the data have positive labels, making this a challenging classification problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import boto3\n",
    "import io\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np \n",
    "import os\n",
    "import pandas as pd \n",
    "\n",
    "import sagemaker\n",
    "import sagemaker.amazon.common as smac\n",
    "from sagemaker import get_execution_role\n",
    "from sagemaker.predictor import csv_serializer, json_deserializer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "isConfigCell": true
   },
   "outputs": [],
   "source": [
    "# Set data locations\n",
    "bucket = '<your_s3_bucket_here>'                        # replace this with your own bucket\n",
    "prefix = 'sagemaker/DEMO-linear-learner-loss-weights'   # replace this with your own prefix\n",
    "s3_train_key = '{}/train/recordio-pb-data'.format(prefix)\n",
    "s3_train_path = os.path.join('s3://', bucket, s3_train_key)\n",
    "local_raw_data = 'creditcard.csv.zip'\n",
    "role = get_execution_role()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Confirm access to s3 bucket\n",
    "for obj in boto3.resource('s3').Bucket(bucket).objects.all():\n",
    "    print(obj.key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Read the data, shuffle, and split into train and test sets, separating the labels (last column) from the features\n",
    "raw_data = pd.read_csv(local_raw_data).as_matrix()\n",
    "np.random.seed(0)\n",
    "np.random.shuffle(raw_data)\n",
    "train_size = int(raw_data.shape[0] * 0.7)\n",
    "train_features  = raw_data[:train_size, :-1]\n",
    "train_labels = raw_data[:train_size, -1]\n",
    "test_features = raw_data[train_size:, :-1]\n",
    "test_labels = raw_data[train_size:, -1]\n",
    "\n",
    "# Convert the processed training data to protobuf and write to S3 for linear learner\n",
    "vectors = np.array([t.tolist() for t in train_features]).astype('float32')\n",
    "labels = np.array([t.tolist() for t in train_labels]).astype('float32')\n",
    "buf = io.BytesIO()\n",
    "smac.write_numpy_to_dense_tensor(buf, vectors, labels)\n",
    "buf.seek(0)\n",
    "boto3.resource('s3').Bucket(bucket).Object(s3_train_key).upload_fileobj(buf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll wrap the model training setup in a convenience function that takes in the S3 location of the training data, the model hyperparameters that define our training job, and the S3 output path for model artifacts.  Inside the function, we'll hardcode the algorithm container, the number and type of EC2 instances to train on, and the input and output data formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sagemaker.amazon.amazon_estimator import get_image_uri\n",
    "\n",
    "def predictor_from_hyperparams(s3_train_data, hyperparams, output_path):\n",
    "    \"\"\"\n",
    "    Create an Estimator from the given hyperparams, fit to training data, and return a deployed predictor\n",
    "    \"\"\"\n",
    "    # specify algorithm containers and instantiate an Estimator with given hyperparams\n",
    "    container = get_image_uri(boto3.Session().region_name, 'linear-learner')\n",
    "\n",
    "    linear = sagemaker.estimator.Estimator(container,\n",
    "        role,\n",
    "        train_instance_count=1,\n",
    "        train_instance_type='ml.m4.xlarge',\n",
    "        output_path=output_path,\n",
    "        sagemaker_session=sagemaker.Session())\n",
    "    linear.set_hyperparameters(**hyperparams)\n",
    "    # train model\n",
    "    linear.fit({'train': s3_train_data})\n",
    "    # deploy a predictor\n",
    "    linear_predictor = linear.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')\n",
    "    linear_predictor.content_type = 'text/csv'\n",
    "    linear_predictor.serializer = csv_serializer\n",
    "    linear_predictor.deserializer = json_deserializer\n",
    "    return linear_predictor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And add another convenience function for setting up a hosting endpoint, making predictions, and evaluating the model.  To make predictions, we need to set up a model hosting endpoint.  Then we feed test features to the endpoint and receive predicted test labels.  To evaluate the models we create in this exercise, we'll capture predicted test labels and compare them to actuals using some common binary classification metrics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def evaluate(linear_predictor, test_features, test_labels, model_name, verbose=True):\n",
    "    \"\"\"\n",
    "    Evaluate a model on a test set given the prediction endpoint.  Return binary classification metrics.\n",
    "    \"\"\"\n",
    "    # split the test data set into 100 batches and evaluate using prediction endpoint\n",
    "    prediction_batches = [linear_predictor.predict(batch)['predictions'] for batch in np.array_split(test_features, 100)]\n",
    "    # parse raw predictions json to exctract predicted label\n",
    "    test_preds = np.concatenate([np.array([x['predicted_label'] for x in batch]) for batch in prediction_batches])\n",
    "    \n",
    "    # calculate true positives, false positives, true negatives, false negatives\n",
    "    tp = np.logical_and(test_labels, test_preds).sum()\n",
    "    fp = np.logical_and(1-test_labels, test_preds).sum()\n",
    "    tn = np.logical_and(1-test_labels, 1-test_preds).sum()\n",
    "    fn = np.logical_and(test_labels, 1-test_preds).sum()\n",
    "    \n",
    "    # calculate binary classification metrics\n",
    "    recall = tp / (tp + fn)\n",
    "    precision = tp / (tp + fp)\n",
    "    accuracy = (tp + tn) / (tp + fp + tn + fn)\n",
    "    f1 = 2 * precision * recall / (precision + recall)\n",
    "    \n",
    "    if verbose:\n",
    "        print(pd.crosstab(test_labels, test_preds, rownames=['actuals'], colnames=['predictions']))\n",
    "        print(\"\\n{:<11} {:.3f}\".format('Recall:', recall))\n",
    "        print(\"{:<11} {:.3f}\".format('Precision:', precision))\n",
    "        print(\"{:<11} {:.3f}\".format('Accuracy:', accuracy))\n",
    "        print(\"{:<11} {:.3f}\".format('F1:', f1))\n",
    "        \n",
    "    return {'TP': tp, 'FP': fp, 'FN': fn, 'TN': tn, 'Precision': precision, 'Recall': recall, 'Accuracy': accuracy, \n",
    "             'F1': f1, 'Model': model_name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And finally we'll add a convenience function to delete prediction endpoints after we're done with them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def delete_endpoint(predictor):\n",
    "        try:\n",
    "            boto3.client('sagemaker').delete_endpoint(EndpointName=predictor.endpoint)\n",
    "            print('Deleted {}'.format(predictor.endpoint))\n",
    "        except:\n",
    "            print('Already deleted: {}'.format(predictor.endpoint))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's begin by training a binary classifier model with the linear learner default settings.  Note that we're setting the number of epochs to 40, which is much higher than the default of 10 epochs.  With early stopping, we don't have to worry about setting the number of epochs too high.  Linear learner will stop training automatically after the model has converged."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training a binary classifier with default settings: logistic regression\n",
    "defaults_hyperparams = {\n",
    "    'feature_dim': 30,\n",
    "    'predictor_type': 'binary_classifier',\n",
    "    'epochs': 40\n",
    "}\n",
    "defaults_output_path = 's3://{}/{}/defaults/output'.format(bucket, prefix)\n",
    "defaults_predictor = predictor_from_hyperparams(s3_train_path, defaults_hyperparams, defaults_output_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now we'll produce a model with a threshold tuned for the best possible precision with recall fixed at 90%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training a binary classifier with automated threshold tuning\n",
    "autothresh_hyperparams = {\n",
    "    'feature_dim': 30,\n",
    "    'predictor_type': 'binary_classifier',\n",
    "    'binary_classifier_model_selection_criteria': 'precision_at_target_recall', \n",
    "    'target_recall': 0.9,\n",
    "    'epochs': 40\n",
    "}\n",
    "autothresh_output_path = 's3://{}/{}/autothresh/output'.format(bucket, prefix)\n",
    "autothresh_predictor = predictor_from_hyperparams(s3_train_path, autothresh_hyperparams, autothresh_output_path)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Improving recall with class weights\n",
    "\n",
    "Now we'll improve on these results using a new feature added to linear learner: class weights for binary classification.  We introduced this feature in the *Class Weights* section, and now we'll look into its application to the credit card fraud dataset by training a new model with balanced class weights:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training a binary classifier with class weights and automated threshold tuning\n",
    "class_weights_hyperparams = {\n",
    "    'feature_dim': 30,\n",
    "    'predictor_type': 'binary_classifier',\n",
    "    'binary_classifier_model_selection_criteria': 'precision_at_target_recall', \n",
    "    'target_recall': 0.9,\n",
    "    'positive_example_weight_mult': 'balanced',\n",
    "    'epochs': 40\n",
    "}\n",
    "class_weights_output_path = 's3://{}/{}/class_weights/output'.format(bucket, prefix)\n",
    "class_weights_predictor = predictor_from_hyperparams(s3_train_path, class_weights_hyperparams, class_weights_output_path)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first training examples used the default loss function for binary classification, logistic loss.  Now let's train a model with hinge loss.  This is also called a support vector machine (SVM) classifier with a linear kernel.  Threshold tuning is supported for all binary classifier models in linear learner."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training a binary classifier with hinge loss and automated threshold tuning\n",
    "svm_hyperparams = {\n",
    "    'feature_dim': 30,\n",
    "    'predictor_type': 'binary_classifier',\n",
    "    'loss': 'hinge_loss',\n",
    "    'binary_classifier_model_selection_criteria': 'precision_at_target_recall', \n",
    "    'target_recall': 0.9,\n",
    "    'epochs': 40\n",
    "}\n",
    "svm_output_path = 's3://{}/{}/svm/output'.format(bucket, prefix)\n",
    "svm_predictor = predictor_from_hyperparams(s3_train_path, svm_hyperparams, svm_output_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And finally, let's see what happens with balancing the class weights for the SVM model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training a binary classifier with hinge loss, balanced class weights, and automated threshold tuning\n",
    "svm_balanced_hyperparams = {\n",
    "    'feature_dim': 30,\n",
    "    'predictor_type': 'binary_classifier',\n",
    "    'loss': 'hinge_loss',\n",
    "    'binary_classifier_model_selection_criteria': 'precision_at_target_recall', \n",
    "    'target_recall': 0.9,\n",
    "    'positive_example_weight_mult': 'balanced',\n",
    "    'epochs': 40\n",
    "}\n",
    "svm_balanced_output_path = 's3://{}/{}/svm_balanced/output'.format(bucket, prefix)\n",
    "svm_balanced_predictor = predictor_from_hyperparams(s3_train_path, svm_balanced_hyperparams, svm_balanced_output_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Now we'll make use of the prediction endpoint we've set up for each model by sending them features from the test set and evaluating their predictions with standard binary classification metrics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Evaluate the trained models\n",
    "predictors = {'Logistic': defaults_predictor, 'Logistic with auto threshold': autothresh_predictor, \n",
    "              'Logistic with class weights': class_weights_predictor, 'Hinge with auto threshold': svm_predictor, \n",
    "              'Hinge with class weights': svm_balanced_predictor}\n",
    "metrics = {key: evaluate(predictor, test_features, test_labels, key, False) for key, predictor in predictors.items()}\n",
    "pd.set_option('display.float_format', lambda x: '%.3f' % x)\n",
    "display(pd.DataFrame(list(metrics.values())).loc[:, ['Model', 'Recall', 'Precision', 'Accuracy', 'F1']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results are in!  With threshold tuning, we can accurately predict 85-90% of the fraudulent transactions in the test set (due to randomness in training, recall will vary between 0.85-0.9 across multiple runs).  But in addition to those true positives, we'll have a high number of false positives: 90-95% of the transactions we predict to be fraudulent are in fact not fraudulent (precision varies between 0.05-0.1).  This model would work well as a first line of defense, flagging potentially fraudulent transactions for further review.  If we instead want a model that gives very few false alarms, at the cost of catching far fewer of the fraudulent transactions, then we should optimize for higher precision:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    binary_classifier_model_selection_criteria='recall_at_target_precision', \n",
    "    target_precision=0.9,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And what about the results of using our new feature, class weights for binary classification?  Training with class weights has made a huge improvement to this model's performance!  The precision is roughly doubled, while recall is still held constant at 85-90%.  \n",
    "\n",
    "Balancing class weights improved the performance of our SVM predictor, but it still does not match the corresponding logistic regression model for this dataset.  Comparing all of the models we've fit so far, logistic regression with class weights and tuned thresholds did the best."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Note on target vs. observed recall\n",
    "\n",
    "It's worth taking some time to look more closely at these results.  If we asked linear learner for a model calibrated to a target recall of 0.9, then why didn't we get exactly 90% recall on the test set?  The reason is the difference between training, validation, and testing.  Linear learner calibrates thresholds for binary classification on the validation data set when one is provided, or else on the training set.  Since we did not provide a validation data set, the threshold were calculated on the training data.  Since the training, validation, and test data sets don't match exactly, the target recall we request is only an approximation.  In this case, the threshold that produced 90% recall on the training data happened to produce only 85-90% recall on the test data (due to some randomness in training, the results will vary from one run to the next).  The variation of recall in the test set versus the training set is dependent on the number of positive points. In this example, although we have over 280,000 examples in the entire dataset, we only have 337 positive examples, hence the large difference.  The accuracy of this approximation can be improved by providing a large validation data set to get a more accurate threshold, and then evaluating on a large test set to get a more accurate benchmark of the model and its threshold.  For even more fine-grained control, we can set the number of calibration samples to a higher number.  It's default value is already quite high at 10 million samples:\n",
    "\n",
    "    num_calibration_samples=10000000,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Clean Up\n",
    "\n",
    "Finally we'll clean up by deleting the prediction endpoints we set up:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for predictor in [defaults_predictor, autothresh_predictor, class_weights_predictor, \n",
    "                  svm_predictor, svm_balanced_predictor]:\n",
    "    delete_endpoint(predictor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've just shown how to use the linear learner new early stopping feature, new loss functions, and new class weights feature to improve credit card fraud prediction.  Class weights can help you optimize recall or precision for all types of fraud detection, as well as other classification problems with rare events, like ad click prediction or mechanical failure prediction.  Try using class weights in your binary classification problem, or try one of the new loss functions for your regression problems: use quantile prediction to put confidence intervals around your predictions by learning 5% and 95% quantiles.  For more information about new loss functions and class weights, see the linear learner [documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/linear-learner.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### References\n",
    "Andrea Dal Pozzolo, Olivier Caelen, Reid A. Johnson and Gianluca Bontempi. Calibrating Probability with Undersampling for Unbalanced Classification. In Symposium on Computational Intelligence and Data Mining (CIDM), IEEE, 2015.  See link to full license text on [Kaggle](https://www.kaggle.com/mlg-ulb/creditcardfraud)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.  Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
